package com.hxht.storage.server.dao.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.hxht.storage.server.dao.NvrDiskDao;
import com.hxht.storage.server.pojo.NvrDiskInfo;

@Repository
public class NvrDiskDaoImpl extends BaseDaoImpl<NvrDiskInfo> implements NvrDiskDao {

	@PostConstruct
	@Override
	public void init() {
		clazz = NvrDiskInfo.class;
	}

	@Override
	public NvrDiskInfo findByNvr(String nvrIp, int diskIndex) {
		
		NvrDiskInfo disk = null;
		
		TypedQuery<NvrDiskInfo> query = entityManage.createQuery(
				"select disk from NvrDiskInfo as disk left join disk.nvr "
				+ "as nvr  where nvr.ip=:ip and disk.diskIndex=:diskIndex", clazz)
				.setParameter("ip", nvrIp).setParameter("diskIndex", diskIndex);
		
		List<NvrDiskInfo> diskList = query.getResultList();
		
		if (diskList != null && diskList.size() != 0){
			disk = diskList.get(0);
		}
		
		return disk;
	}

}
